package com.sh.data.engine.web.webapi.controller.management;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.management.ApplicationKeyDTO;
import com.sh.data.engine.application.model.request.management.SaveAkSkRequest;
import com.sh.data.engine.application.model.request.management.UpdateAkSkRequest;
import com.sh.data.engine.application.service.management.AccessKeyApplicationService;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@RequestMapping("/management/AKSK")
@Tag(name = "数据集成-AKSK管理", description = "AKSK管理API")
public class ApplicationKeyController {

    @Autowired
    private AccessKeyApplicationService accessKeyApplicationService;

    @PostMapping("/createAccessKey")
    @Operation(summary = "创建AkSk")
    @LogExecution(operation = EngineOperation.ADD, business = Business.个人中心)
    public RestResponseEntity<ApplicationKeyDTO> createAccessKey(@RequestBody SaveAkSkRequest request) {
        return RestResponseEntity.success(accessKeyApplicationService.createAccessKey(request));
    }

    @PostMapping("/deleteAccessKey")
    @Operation(summary = "删除AkSk")
    @LogExecution(operation = EngineOperation.DEL, business = Business.个人中心)
    public RestResponseEntity<Integer> deleteAccessKey(@RequestBody String accessKey) {
        return RestResponseEntity.success(accessKeyApplicationService.deleteAccessKey(accessKey));
    }

    @PostMapping("/updateAKState")
    @Operation(summary = "更新AkSk状态")
    @LogExecution(operation = EngineOperation.UPDATE, business = Business.个人中心)
    public RestResponseEntity<Integer> updateAKState(@RequestBody String accessKey) {
        return RestResponseEntity.success(accessKeyApplicationService.updateAKState(accessKey));
    }

    @PostMapping("/updateDescription")
    @Operation(summary = "编辑描述")
    @LogExecution(operation = EngineOperation.UPDATE, business = Business.个人中心)
    public RestResponseEntity<Integer> updateDescription(@RequestBody UpdateAkSkRequest request) {
        return RestResponseEntity.success(accessKeyApplicationService.updateDescription(request));
    }

    @GetMapping("/getAccessKey")
    @Operation(summary = "获取AkSk列表")
    @LogExecution(operation = EngineOperation.SELECT, business = Business.个人中心)
    public RestResponseEntity<PagerResponse<ApplicationKeyDTO>> getAccessKey(
            @Parameter(description = "当前页", schema = @Schema(defaultValue = "1"))
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小", schema = @Schema(defaultValue = "10"))
            @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ) {
        PagerResponse<ApplicationKeyDTO> result = accessKeyApplicationService.getAccessKey(pageNum, pageSize);
        return RestResponseEntity.success(result);
    }

}
